import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ConfirmBox extends Dialog {
  String title;
  bool isTitle;//是否需要标题
  bool iserrFn;//是否需要取消回调
  String? content;
  String leftBtnText = '取消';
  String rightBtnText = '确定';
  Function? succFn;
  Function? errFn;//取消回调
  bool isAlert;
  bool isPlanSelectCourse;

  ConfirmBox({
    Key? key,
    String? title,
    bool? isTitle,//是否需要标题
    bool? iserrFn,//是否需要取消回调
    bool? isAlert,
    bool? isPlanSelectCourse,
    String? content,
    String? leftBtnText,
    String? rightBtnText,
    this.succFn,
    this.errFn,//取消回调
  }):title = title == null ? '确认' : title,
        content = content ,
        leftBtnText = leftBtnText == null ? '取消' : leftBtnText,
        rightBtnText = rightBtnText == null ? '确定' : rightBtnText,
        isTitle= isTitle == null ? true : isTitle,
        iserrFn= iserrFn == null ? false : iserrFn,
        isAlert = isAlert == null ? false : isAlert,
        isPlanSelectCourse = isPlanSelectCourse == null ? false : isPlanSelectCourse,
  super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Material(
      type: MaterialType.transparency,
      child: Container(
        child: Center(
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 40.w),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(10)),
                color: Colors.white),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
               Visibility(child:  Container(
                 margin: EdgeInsets.only(top: 30.h,bottom: content==null?30.h:0),
                 child: Text(
                   title,
                   textAlign: TextAlign.center,
                   overflow: TextOverflow.ellipsis,
                   style: TextStyle(
                       fontSize: 34.sp,
                       fontWeight: FontWeight.bold,
                       color: Color(0xFF333333)),
                 ),
               ),visible: isTitle,),
                Visibility(child: Container(
                  alignment: Alignment.center,
                  padding:
                  EdgeInsets.symmetric(horizontal: 40.w, vertical: 30.h),
                  child: Text(
                    content??'',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 26.sp,
                        height: 1.8,
                        color: Color(0xFF666666)),
                  ),
                ),visible: content!=null,),
                Visibility(
                  child:Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        GestureDetector(
                          onTap: (){

                          },
                          child: Container(
                            height: 30.h,
                            alignment: Alignment.center,
                            child: Icon(Icons.check_circle,color: Color(0xff999999),size: 30.w,),
                          ),
                        ),
                        SizedBox(width: 10.w,),
                        Container(
                          height: 30.h,
                          alignment: Alignment.center,
                          child: Text('不再提示',style: TextStyle(
                            color: Color(0xff333333),
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w600
                          ),),
                        ),
                      ],
                    ),
                    margin: EdgeInsets.only(bottom: 40.w),
                  ),
                  visible: isPlanSelectCourse==true,
                ),
                Container(
                  height: 1.w,
                  width: double.infinity,
                  color: Color(0xFFE7E7E7),
                ),
                Visibility(child: Row(
                  children: <Widget>[
                    Expanded(child: Container(
                      height: 90.h,
                      child: GestureDetector(
                        onTap: () {
                          if(!iserrFn){
                            Navigator.pop(context);
                          }else{
                            Navigator.pop(context);
                            if (errFn != null) {
                              errFn!();
                            }
                          }


                        },
                        child: Text(leftBtnText,style: TextStyle(
                          color: Color(0xFF666666)
                        ),),
                      ),
                    ),),
                    Container(
                      height: 90.h,
                      width: 1.w,
                      color: Color(0xFFE7E7E7),
                    ),
                    Expanded(child: Container(
                      height: 90.h,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                          if (succFn != null) {
                            succFn!();
                          }
                        },
                        child: Text(rightBtnText,style: TextStyle(
                          color: Color(0xFFFF3000)
                        ),),
                      ),
                    ),)
                  ],
                ),visible: isAlert==false,),
                Visibility(child: Row(
                  children: <Widget>[
                    Expanded(child: Container(
                      height: 90.h,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.pop(context);
                          if (succFn != null) {
                            succFn!();
                          }
                        },
                        child: Text(rightBtnText,style: TextStyle(
                            color: Color(0xFFFF3000)
                        ),),
                      ),
                    ),)
                  ],
                ),visible: isAlert==true,),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

void openConfirm(
  context, {
  String? title,
  bool? isTitle,//是否需要标题
  bool? iserrFn,//是否需要取消回调
  String? content,
  String? leftBtnText,
  String? rightBtnText,
  Function? succFn,
  Function? errFn,//取消回调
  bool isAlert = false,//是否只展示我知道了
  bool isPlanSelectCourse = false,
  bool barrierDismissible = false,
}) {
  showDialog<Null>(
      context: context, //BuildContext对象
      barrierDismissible: barrierDismissible,
      builder: (BuildContext context) {
        return ConfirmBox(
          title: title,
          isTitle:isTitle,
          iserrFn:iserrFn,
          content: content,
          leftBtnText: leftBtnText,
          rightBtnText: rightBtnText,
          succFn: succFn,
          errFn:errFn,
          isAlert:isAlert,
          isPlanSelectCourse:isPlanSelectCourse
        );
      });
}



